package cn.edu.nit.sys.controller;

import cn.edu.nit.sys.config.OperationAnnotation;
import cn.edu.nit.sys.entity.Menu;
import cn.edu.nit.sys.service.MenuService;
import cn.edu.nit.sys.utils.R;
import cn.edu.nit.sys.vo.MenuVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @Author xiaobai
 * @Date 2022/8/24 22:00
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/routes")
    @OperationAnnotation(description = "获取菜单", opType = "查询")
    public R getRoutes(@PathParam("uid")String uid) {
        return menuService.getMenus(uid);
    }

    @GetMapping
    @OperationAnnotation(description = "获取菜单", opType = "查询")
    public R getMenuList(Page<Menu> page, String name) {
        return menuService.getMenuList(page, name);
    }

    @GetMapping("/all")
    public R getAllMenu() {
        return  menuService.getAll();
    }

    @PostMapping
//    @PreAuthorize("hasAuthority('ADMIN')")
    @OperationAnnotation(description = "添加菜单", opType = "添加")
    public R addMenu(@RequestBody MenuVO menu) {
        return menuService.addMenu(menu);
    }

    @PutMapping
//    @PreAuthorize("hasAuthority('ADMIN')")
    @OperationAnnotation(description = "修改菜单", opType = "修改")
    public R updateMenu(@RequestBody MenuVO menuVO) {
        return menuService.updateMenu(menuVO) ? R.ok("修改成功") : R.error("修改失败");
    }

    @DeleteMapping("/{id}")
//    @PreAuthorize("hasAuthority('ADMIN')")
    @OperationAnnotation(description = "删除菜单", opType = "删除")
    public R delMenu(@PathVariable("id")Integer id) {
        return menuService.delMenu(id);
    }


    @PutMapping("/lock/{id}")
//    @PreAuthorize("hasAuthority('ADMIN')")
    @OperationAnnotation(description = "锁定菜单", opType = "修改")
    public R lockMenu(@PathVariable("id")Integer id) {
        return menuService.lockMenu(id) ? R.ok("操作成功") : R.error("操作失败");
    }

}
